package com.example.demo.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * Created on 2019/9/14.
 *
 * @author Grak
 * @since 1.0
 */
@Component
public class SendMail {

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 普通发送
     */
    public void sendBaseMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("591471019@qq.com");
        message.setTo("591471019@qq.com");
        message.setSubject("这是主题");
        message.setText("这是具体内容");
        javaMailSender.send(message);
    }

    /**
     * 带附件
     * @throws MessagingException
     */
    public void sendFileMail() throws MessagingException {
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message,true);
        helper.setFrom("591471019@qq.com");
        helper.setTo("591471019@qq.com");
        helper.setSubject("这是主题");
        helper.setText("这是具体内容");

        //附件内容
        FileSystemResource file = new FileSystemResource(new File("C:\\Users\\Admin\\Desktop\\早1.png"));
        helper.addAttachment("早1.png",file);
        javaMailSender.send(message);
    }
}
